package club.bigtian.sync.controller;

import club.bigtian.sync.config.SyncConfig;
import club.bigtian.sync.model.TableSchema;
import club.bigtian.sync.service.SyncService;
import club.bigtian.sync.service.MySQLToClickHouseSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 同步控制器
 * 提供REST API接口，用于管理同步任务
 */
@Slf4j
@RestController
@RequestMapping("/api/sync")
public class SyncController {

    @Autowired
    private SyncService syncService;
    
    @Autowired
    private SyncConfig syncConfig;
    
    @Autowired
    private MySQLToClickHouseSyncService mySQLToClickHouseSyncService;
    
    /**
     * 初始化同步环境
     */
    @PostMapping("/init")
    public ResponseEntity<Map<String, Object>> init() {
        log.info("接收到初始化同步环境请求");
        
        try {
            syncService.init();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "同步环境初始化成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("初始化同步环境失败: {}", e.getMessage(), e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "初始化同步环境失败: " + e.getMessage());
            
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 执行全量同步
     */
    @PostMapping("/full/{tableName}")
    public ResponseEntity<Map<String, Object>> fullSync(@PathVariable String tableName) {
        log.info("接收到执行全量同步请求: {}", tableName);
        
        try {
            syncService.fullSync(tableName);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "全量同步执行成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("全量同步执行失败: {}", e.getMessage(), e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "全量同步执行失败: " + e.getMessage());
            
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 开始增量同步
     */
    @PostMapping("/incremental/start")
    public ResponseEntity<Map<String, Object>> startIncrementalSync() {
        log.info("接收到开始增量同步请求");
        
        try {
            syncService.startIncrementalSync();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "增量同步已开始");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("开始增量同步失败: {}", e.getMessage(), e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "开始增量同步失败: " + e.getMessage());
            
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 停止增量同步
     */
    @PostMapping("/incremental/stop")
    public ResponseEntity<Map<String, Object>> stopIncrementalSync() {
        log.info("接收到停止增量同步请求");
        
        try {
            syncService.stopIncrementalSync();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "增量同步已停止");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("停止增量同步失败: {}", e.getMessage(), e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "停止增量同步失败: " + e.getMessage());
            
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 获取表结构
     */
    @GetMapping("/table/{tableName}")
    public ResponseEntity<TableSchema> getTableSchema(@PathVariable String tableName) {
        log.info("接收到获取表结构请求: {}", tableName);
        
        try {
            TableSchema tableSchema = syncService.getTableSchema(tableName);
            return ResponseEntity.ok(tableSchema);
        } catch (Exception e) {
            log.error("获取表结构失败: {}", e.getMessage(), e);
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    /**
     * 获取同步状态
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getSyncStatus() {
        log.info("接收到获取同步状态请求");
        
        try {
            String status = syncService.getSyncStatus();
            
            Map<String, Object> response = new HashMap<>();
            response.put("status", status);
            response.put("mode", syncConfig.getSyncMode());
            response.put("tables", syncConfig.getTableList());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取同步状态失败: {}", e.getMessage(), e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取同步状态失败: " + e.getMessage());
            
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 执行MySQL到ClickHouse的全量同步
     */
    @PostMapping("/mysql/full/{tableName}")
    public ResponseEntity<Map<String, Object>> mysqlFullSync(@PathVariable String tableName) {
        log.info("接收到MySQL到ClickHouse的全量同步请求，表名：{}", tableName);
        mySQLToClickHouseSyncService.fullSync(tableName);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        return ResponseEntity.ok(response);
    }
} 